package com.fll.util;

import com.fll.config.FinalFieldConfig;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author fanlei
 * @version 1.0
 * @date 2018年09月14日
 */
public class CrawUtil {

    public static final Map<String,String> specialStrs;

    static {
        //替换名称中的特殊字符，不然win系统创建文件时会报错
        specialStrs = new HashMap<String, String>();
        specialStrs.put("\\", " ");
        specialStrs.put(" / ", "-");
        specialStrs.put("/", "-");
        specialStrs.put(":", " ");
        specialStrs.put("*", " ");
        specialStrs.put("?", " ");
        specialStrs.put("\"", "'");
        specialStrs.put("<", "(");
        specialStrs.put(">", ")");
        specialStrs.put("|", " ");
    }

    /**
     * 特殊字符替换
     * @param str
     * @return
     */
    public static String StrDispose(String str){
        Set<String> keys = specialStrs.keySet();
        for (String key:
                keys) {
            str = str.replace(key,specialStrs.get(key));
        }
        return str;
    }

    /**
     * 提取字符串中的数字
     * @param str
     * @return
     */
    public static Integer IntByStr(String str){
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return Integer.valueOf(m.replaceAll("").trim());
    }


    /**
     * 拼接输出路径
     * @param names
     * @param type
     * @param name
     * @return
     */
    public static String joinStr(List<String> names, String type, String... name){
        StringBuffer sb = new StringBuffer(FinalFieldConfig.outFilePath);
        for (String s:
                names) {
            sb.append("/" + s);
        }
        for (String n:
                name) {
            sb.append("/" + n);
        }
        sb.append("《" + type + "》");
        return sb.toString();
    }
    //创建文件夹
    public static void mkdirsFile(String mkdirPath){
//        File file = new File("D:/极客学院/ddd");
        File file = new File(mkdirPath);
        if(!file.exists()){//不存在
            try {
                file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void deleteFile(String path){
        File file = new File(path);
        if(file.exists()) {//存在
            file.delete();
        }
    }


    /**
     * 序列化
     * @param path
     * @param o
     */
    public static void serializer(String path,Object o){
        mkdirsFile(FinalFieldConfig.outFilePath);
//        createNewFile(path);

        ObjectOutputStream stream = null;
        try {
            stream = new ObjectOutputStream(new FileOutputStream(FinalFieldConfig.outFilePath + path));
            stream.writeObject(o);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (stream != null){
                try {
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 反序列化
     * @param path
     */
    public static Object reSerializer(String path) {
        ObjectInputStream stream = null;
        Object o = null;
        try {
            File file = new File(FinalFieldConfig.outFilePath + path);
            if(!file.exists()) {//不存在
                return null;
            }
            stream = new ObjectInputStream(new FileInputStream(FinalFieldConfig.outFilePath + path));
            o = stream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (stream != null){
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return o;
    }



}
